var util = require('../../utils/util');
var wxApi = require('../../utils/wxApi');
var wxRequest = require('../../utils/wxRequest');
import config from '../../utils/config';
var app = getApp();
var that;

//获取应用实例
var app = getApp()
Page({
  data: {
    user: {}
    ,pageInex:1
    ,isload:0
    ,tid:0
    ,team:null
    ,group:[]
    ,sgid:0
    ,showgroup:true
    ,userall:0
    ,stepall:0
    ,team_pm:[]
    ,group_index: 0

    ,isLoadMore : false
    ,isEnd : false
    ,isEmpty:false

    ,cjt:0
    ,yjt:0
    ,cjtstr:"距离比赛结束"
    ,wks:true
    ,rens:0
    ,list:[]
    ,rens:0
    ,step:0
    ,pageInex:1
    ,joingroup:false
    ,zpm:0
    ,tpm:0
    ,hasgroup:true
    ,gname:""
    ,gid :0
    ,zrs:0
    ,zbs:0
    ,isdown:false
  },
  onPullDownRefresh:function(){
      that.setData({
          list : [],
          isLoadMore : false,
          isEnd : false,
          isEmpty:false,
          isdown:true,
          pageInex:1
      });
      that.loadData();
  },
  onShareAppMessage: function () {
      return app.getShare();
  },
  onShow:function(){
   
  },
  onReady:function(){
    that.data.isload=0;
    
  },
  onReachBottom : function(e,a){
    that.loadList();
  },
  onLoad: function (options) {
    that = this;
    that.data.isload=1;
    that.data.pageInex=1;
    if( !util.judge.isNull(options.tid) ){
      that.data.tid = parseInt(options.tid);
    }
    that.loadData();
  },
  
  loadData: function () {
    util.showLoading();
    app.getUserInfo()
    .then(res => {
        console.log(res);
      that.setData({
        user:res
      })
      
      var url = config.url_team_get;
      var params = {
        id:that.data.tid,
        code:''
      }
      return wxRequest.postRequest(url, params)
    })
    .then(res => {
        console.log(res);
        if(util.judge.isObject(res)){

          var team =res.team;
          that.data._sdesc="D几名邀请您参加\""+team.name+ "\"比赛！" ;
          wx.setNavigationBarTitle( { title: team.name });

          var bd = util.str2date(team.bdate);
          var ed = util.str2date(team.edate);

          res.team.bdate = util.date2str(team.bdate,"MM.dd");
          res.team.edate = util.date2str(team.edate,"MM.dd");

          var d1 = new Date();    //结束时间  
            
          
            if(d1.getTime() > ed.getTime()){
                that.setData({cjtstr:"比赛已结束",wks:false});
            }
            else if(bd.getTime() > d1.getTime()){
                that.setData({cjtstr:"比赛未开始",wks:false});
            }
            else{
                //进行中
                var date3 = ed.getTime() - d1.getTime();   //时间差的毫秒数 
                var days=parseFloat(date3/(24*3600*1000));  
                if(days < 1){
                    that.setData({cjtstr:"比赛今天结束",wks:false});
                }else{
                    days=that.modFoat(days);
                    that.setData({ cjtstr: "距离比赛结束", wks: true });
                }
           }

          date3 = ed.getTime() - bd.getTime();   //时间差的毫秒数        
          var yjt = parseInt(parseFloat(date3/(24*3600*1000) + 0.00001))+1; 

          that.setData({cjt:days,yjt:yjt});

          that.setData({
            team:res.team,
            stepall:res.stepall,
            userall:res.userall,
            team_pm:res.pm
            ,zrs:res.team.usernum
          });

          that.loadList();
        }
    })
    .catch(res => {
        console.log(res);
        util.alert_catch(res.message);
    })
    .finally(function (res) {
        util.hideLoading()
        wx.stopPullDownRefresh();
    });
    
  },

  loadList: function(reload){
      that.setData({
          isLoadMore : true
      });

      var id= that.data.tid;
      var url = config.url_team_user;
      var params = {
          pageIndex: that.data.pageInex,
          pageSize: app.globalData.defaultPageSize,
          id:id,
          gid : 0
      }

      wxRequest.postRequest(url, params)
      .then(res=>{
      
          if(that.data.pageInex==1 && res.length==0){
              that.setData({
                  isLoadMore : false,
                  isEnd : false,
                  isEmpty :true,
              });
          }
      
          if(res.length > 0){
              if(that.data.isdown==true){
                  that.data.list =[];
                  that.data.isdown=false;
              }

              var data = that.data.list.concat(res);
              that.setData({
                  list : data,
                  isLoadMore : false
              });
              that.data.pageInex++;
          }
      
          if(res.length < app.globalData.defaultPageSize || res.length == 0 ){
              that.setData({
                  isLoadMore : false,
                  isEnd : true
              });
          }

      })
      .catch(res => {
          util.alert(res.message);
      })
      .finally(function (res) {
          util.hideLoading();
      });
    

  },
  ontc:function(e){
      var data = e.currentTarget.dataset;

      util.showLoading();
      var url = config.url_team_user_out;
      var params = {
          id:data.id
          ,ot:data.tc
          ,tid:that.data.tid
      }
      wxRequest.postRequest(url, params)
      .then(res => {
          if(!util.judge.isNull(res)){
              this.onPullDownRefresh();
          }
      })
      .catch(res => {
          util.alert(res.message);
      })
      .finally(function (res) {
          util.hideLoading();
      });

  },
  modFoat:function(v) {
        var _max = parseInt(v) + 1;
        if( _max - v < 1 ) {
            return _max;
        }
        return v;
    } 

})